package com.sh.suhe.utildemos.views.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.sh.suhe.utildemos.R;
import com.sh.suhe.utildemos.R2;
import com.sh.suhe.utildemos.model.PaPhotoModel;
import com.sh.suhe.utildemos.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class PaPhotoAdapter extends RecyclerView.Adapter<PaPhotoAdapter.ViewHolder> {
    List<PaPhotoModel> imgList = new ArrayList<>();
    Context mContext;

    public PaPhotoAdapter(Context context){
        mContext = context;
    }
    @NonNull
    @Override
    public PaPhotoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pa_photo,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull PaPhotoAdapter.ViewHolder holder, int position) {
        String img = imgList.get(position).getImg();

        //2列的瀑布流，屏幕宽度减去两列间的间距space所的值再除以2，计算出单列的imageview的宽度，space的值在RecyclerView初始化时传入
        Glide.with(mContext)
                .load(img).dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.iv);
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageClick != null) {
                    imageClick.clickImg(img, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imgList.size();
    }
    public void setImgList(List<PaPhotoModel> imgs){
        imgList.clear();
        imgList.addAll(imgs);
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.item_pa_photo_iv);
        }
    }
    ImageClick imageClick;
    public void setImageClick(ImageClick imageClick){
        this.imageClick = imageClick;
    }

    public interface ImageClick {
        void clickImg(String url, int position);
    }
}
